﻿using System;
using System.Diagnostics;
using System.Collections.Generic;

//svnkit imports
using org.tmatesoft.svn.core;
using org.tmatesoft.svn.core.wc;

namespace Subsync.Core.Subversion
{
    class SVNCleanup : CommandBase
    {
        // Initialise variables with default settings.
        java.io.File filePath;

        // Constructor
        public SVNCleanup(string path)
        {
            Debug.WriteLine("SVNLock Constructor");
            setupCleanup(path);
        }

        public void setupCleanup(string path)
        {
            this.filePath = new java.io.File(path);
        }

        public override void Execute()
        {
            Debug.WriteLine("SVNLock Execution");

            //library setup
            Tools.setupLibrary();

            //load up the clients
            SVNClientManager cm = SVNClientManager.newInstance();
            SVNWCClient wc = cm.getWCClient();

            try
            {
                wc.doCleanup(this.filePath);
            }
            catch (SVNException e)
            {
                throw new SVNExecuteException(e.getMessage());
            }
        }

        public override Dictionary<string, List<object>> getTokens ()
		{
			Dictionary<string, List<object>> ret = new Dictionary<string, List<object>> ();
			ret.Add("Cleanup",new List<object>() {"cu","cleanup"});
			return ret;
		}
		
		public override List<KeyValuePair<string,initPointer>> getInitPointers() 
        {
			List<KeyValuePair<string,initPointer>> ret = new List<KeyValuePair<string,initPointer>>();
			ret.Add(new KeyValuePair<string,initPointer>("Cleanup",new initPointer(this.handleCLI)));
			return ret;
		}
		
		public void handleCLI(Dictionary<string,List<object>> tokens) 
        {
			if (tokens["Cleanup"].Count == 0) {
				Console.WriteLine("Not enough arguments: Cleanup requires at least 1 argument (path)");
				return;
			}
            string strPath = (string) tokens["Cleanup"][0];
			this.setupCleanup(strPath);
            this.Execute();
        }

        public SVNCleanup() {}
    }
}
